import { pageCuesActive } from "../../../service/api/api-cues-active";
import { calculateDaysAgo } from "../../../utils/util";
import Message from 'tdesign-miniprogram/message/index';
// pages/cues/active/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    access: {} as any,// 当前用户权限
    navBarHeight: 0,//导航栏高度
    currentUser: {},//当前用户完整信息
    activeList: [],//选题审批列表
    handleName: '',//操作名称
    handleContent: '',//操作提示描述
    handelDescribes: '',//操作描述
    buttonRecordState: 0,//操作按钮状态
    grade: 1,//分类                      
    currentPage: 1, // 当前页数
    pageSize: 8, // 每页数据数量
    hasMoreData: true, // 是否还有更多数据
    total: 0,//所有数据条数
    firstReachBottom: 0,//是否第一次触底
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    this.onRefresh();
  },
  async onRefresh() {
    //导航条加载动画
    wx.showNavigationBarLoading();
    //停止下拉刷新
    const response = await pageCuesActive({ current: 1, pageSize: 8 });
    if (response.code === 0) {
      const hasMoreData = response.data.total >= 8;
      const list = response.data.data
      list.forEach(item => {
        item.beforNow = calculateDaysAgo(item.createdTime)
      })
      this.setData({
        activeList: list,
        hasMoreData,
        currentPage: 1,
        pageSize: 8,
        total: response.data.total,
      })
      setTimeout(() => {
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }, 1000);
    }
  },
  // 下拉加载更多数据
  onReachBottom() {
    this.setData({
      firstReachBottom: this.data.firstReachBottom + 1
    })
    if (this.data.total > this.data.pageSize) {
      wx.showLoading({
        title: '加载中...'
      })
      if (this.data.hasMoreData) {
        const nextPage = this.data.currentPage + 1;
        this.setData({
          currentPage: nextPage
        })
        this.getActiveListPage({ current: nextPage, pageSize: this.data.pageSize })
        wx.hideLoading()
      } else {
        if (this.data.firstReachBottom === this.data.currentPage) {
          wx.showToast({
            title: '已加载完成'
          })
        }
        wx.hideLoading()
      }
    }
  },
  //获取选题列表用于下拉加载更多
  async getActiveListPage(params: { current: number, pageSize: number }) {
    wx.showLoading({
      title: '加载中...'
    })
    try {
      const response = await pageCuesActive({ current: params.current, pageSize: params.pageSize });
      const hasMoreData = response.data.total >= params.pageSize * params.current;
      const list = response.data.data
      list.forEach(item => {
        item.beforNow = calculateDaysAgo(item.createdTime)
      })
      this.setData({
        activeList: [...this.data.activeList, ...list],
        total: response.data.total,
        hasMoreData
      })
      wx.hideLoading()
    } catch (error) {
      wx.hideLoading()
    }
  },
  //页面刷新
  async getActiveList(params: { current: number, pageSize: number }) {
    const response = await pageCuesActive({ current: params.current, pageSize: params.pageSize });
    const list = response.data.data
    list.forEach(item => {
      item.beforNow = calculateDaysAgo(item.createdTime)
    })
    if (response.code === 0) {
      this.setData({
        activeList: list,
        total: response.data.total,
      })
    }
  },
  //打开主动上报create
  handleCreateActive() {
    wx.navigateTo({
      // url: '/pages/cues/active/create/index?mode=' + this.data.mode
      url: '/pages/cues/active/create/index'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const { globalData: { currentUser, navBarHeight, access } } = getApp();
    this.setData({
      currentUser,
      navBarHeight,
      access
    })
    this.getActiveList({ current: this.data.currentPage, pageSize: this.data.pageSize })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {

    let page = wx.getStorageSync('active-page');
    if (page == 'detail') {
      let index = wx.getStorageSync('active-detail-index')
      let detailData = wx.getStorageSync('active-detail-data');
      const oldData = this.data.instructList[index];
      // this.setData({
      //   [`instructList[${index}].beforNow`]: '哈哈哈哈',
      // })
    } else if (page == "create") {
      let add = wx.getStorageSync('active-create-add')
      if (add) {
        this.getActiveList({ current: 1, pageSize: 8 })
      } else {
      }
    } else if (page == "address") {
    } else {
    }
    // if (!this.data.access.showCuesActive) {

    // } else {
    //   Message.warning({
    //     context: this,
    //     offset: [20, 32],
    //     duration: 5000,
    //     content: '暂无权限',
    //   });
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload() {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})